package com.sgb.kjwl.view.ui.activity.packers;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityServicesCompanyBinding;
import com.sgb.kjwl.model.entity.ServicesCompanyEntity;
import com.sgb.kjwl.model.entity.packers.PickResultEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.ui.adapter.packers.ServicesCompanyAdapter;
import com.sgb.kjwl.view.widget.PickPopWindow1;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.viewmodel.packers.ServicesCompanyModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;
import java.util.Objects;

/**
 * @author 李昭
 * @date 2020/9/18
 * description:劳务公司
 */
public class ServicesCompanyActivity extends AppActivity {

    ServicesCompanyModel viewModel;
    private ActivityServicesCompanyBinding binding;
    private ServicesCompanyAdapter servicesCompanyAdapter;

    /**
     * 企业类型
     */
    private PickPopWindow1 popType;
    private PickResultEntity pickResultType;
    /**
     * 所在区域
     */
    private ThreeLevelAreaWindow popCity;
    private PickResultEntity pickResultProvince;
    private PickResultEntity pickResultCity;
    private PickResultEntity pickResultArea;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ServicesCompanyActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
//        DaggerPackersComponent.builder()
//                .appComponent(appComponent)
//                .packersModule(new PackersModule(this))
//                .build()
//                .inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    private void initFilterData(boolean isLoadMore) {
        viewModel.getNewList(pickResultType.getName(), pickResultProvince.getNo(), pickResultCity.getNo(), pickResultArea.getNo(), isLoadMore);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_services_company);

        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.shape_list_divide)));
        binding.recyclerView.addItemDecoration(divider);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initFilterData(false);
            }

            @Override
            public void onLoadMore() {
                initFilterData(true);
            }
        });

        if (servicesCompanyAdapter == null) {
            servicesCompanyAdapter = new ServicesCompanyAdapter(this, null);
        }
        servicesCompanyAdapter.setOnItemClick(new AdapterOnItemClick<ServicesCompanyEntity>() {
            @Override
            public void onItemClick(ServicesCompanyEntity servicesCompanyEntity, int position) {
                if (TextUtils.isEmpty(servicesCompanyEntity.getId())) {
                    ToastUtil.show(getActivity(), "企业店铺id不能为空");
                    return;
                }
                WebsiteInfoActivity.start(getActivity(), servicesCompanyEntity.getNo(), WebsiteInfoActivity.WEBSITE_FOR_COMPANY);
            }
        });
        binding.recyclerView.setAdapter(servicesCompanyAdapter);

        pickResultType = new PickResultEntity();
        pickResultProvince = new PickResultEntity();
        pickResultCity = new PickResultEntity();
        pickResultArea = new PickResultEntity();
    }

    @Override
    protected void init() {
        //列表数据
//        pickResultProvince.setNo(viewModel.getAreaCode());
//        binding.tvArea.setText(viewModel.getAreaName());
        initFilterData(false);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_company_type) {
            if (popType == null) {
                viewModel.getCompanyTypeModel();
            } else {
                showCompanyTypePop(null);
            }
        } else if (id == R.id.tv_area) {
            if (popCity == null) {
                viewModel.getCityList();
            } else {
                showCityPop(null);
            }
        }
    }

    public void showList(List<ServicesCompanyEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                servicesCompanyAdapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                servicesCompanyAdapter.setDatas(list);
                binding.recyclerView.smoothScrollToPosition(0);
            } else {
                servicesCompanyAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 企业类型pop
     * @param data 企业类型的列表数据
     */
    public void showCompanyTypePop(List<PickSelectEntity> data) {
        if (popType == null) {
            popType = new PickPopWindow1(this);
            popType.setBgView(binding.grayLayout);
            popType.setOnResultClick((PickPopWindow1.OnResultClick) (key1, key2, key3) -> {
                pickResultType.setNo((String) key1);
                pickResultType.setName((String) key2);
                pickResultType.setPosition((int) key3);
                if (TextUtils.isEmpty((String) key2) || TextUtils.equals(getString(R.string.app_all), (String) key2)) {
                    pickResultType.setName("");
                }
                initFilterData(false);
            });
            popType.setOnDismissListener(() -> {
                if (TextUtils.isEmpty(pickResultType.getName()) || TextUtils.equals(getString(R.string.app_all), pickResultType.getName())) {
                } else {
                }
            });

            popType.setData(data);
        }
    }

    /**
     * 省市区pop
     * @param data 地区的列表数据
     */
    public void showCityPop(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        if (popCity == null) {
            popCity = new ThreeLevelAreaWindow(getActivity()){
                @Override
                protected void provinceClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.provinceClick(baseAreaEntity, position);
                    pickResultProvince.setNo(baseAreaEntity.getNo());
                    pickResultProvince.setName(baseAreaEntity.getSimpleName());
                    if (getString(R.string.app_all).equals(baseAreaEntity.getSimpleName()) || pickResultProvince.getName().equals(baseAreaEntity.getSimpleName())){
                        pickResultCity.setNo("");
                        pickResultCity.setName("");
                    }
                }

                @Override
                protected void cityClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.cityClick(baseAreaEntity, position);
                    pickResultCity.setNo(baseAreaEntity.getNo());
                    pickResultCity.setName(baseAreaEntity.getSimpleName());
                    if (getString(R.string.app_all).equals(baseAreaEntity.getSimpleName()) || pickResultCity.getName().equals(baseAreaEntity.getSimpleName())){
                        pickResultArea.setNo("");
                        pickResultArea.setName("");
                    }
                }

                @Override
                protected void areaClick(BaseAreaEntity baseAreaEntity, int position) {
                    super.areaClick(baseAreaEntity, position);
                    pickResultArea.setNo(baseAreaEntity.getNo());
                    pickResultArea.setName(baseAreaEntity.getSimpleName());
                }
            };
            popCity.setBgView(binding.grayLayout);
            popCity.setShowSimpleName(true);
            popCity.setProvinceSingle();
            popCity.setCitySingle();
            popCity.setAreaSingle();
            popCity.setOnResultClick((key1, key2, key3) -> {
                initFilterData(false);
            });
            popCity.setOnDismissListener(() -> {
            });
        }
    }

    private void setTextViewChange(TextView textView, int img, int textColor) {
        Drawable drawable = ContextCompat.getDrawable(this,img);
        if (drawable != null) {
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        }
        textView.setCompoundDrawables(null, null, drawable, null);
        textView.setTextColor(ContextCompat.getColor(this,textColor));
    }
}
